package com.hyc.myvideo.ui.activity.my;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.constants.Constants;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.UserInfo;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.widget.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 编辑昵称
 * @author gyb
 * @version 1.0
 * @created 2017/07/13
 */
public class NicknameActivity extends BaseFActivity {
    /*展示昵称*/
    @BindView(R.id.e_nickname_id) ClearEditText editTextNickname;
    /*保存按钮*/
    @BindView(R.id.head_right_title) TextView textViewSave;
    @Override
    public int getContentViewId() {
        return R.layout.activity_nickname;
    }

    @Override
    protected void initView() {
        setLeftBtnClick(true,getStr(R.string.huc_head_nickname));
        editTextNickname.addTextChangedListener(textWatcher);
        setTextViewSave(false);
        String sign = getIntent().getStringExtra("nickname");
        if (sign != null && sign.trim().length() >= 1) {
            setTextViewSave(true);
            editTextNickname.setText(sign);
            //移动光标到末尾
            editTextNickname.setSelection(editTextNickname.getText().toString().trim().length());
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void oNext(String r, String method) {
        BaseResult<UserInfo> result = JSONObject.parseObject(r, new TypeReference<BaseResult<UserInfo>>() {});
        if(result.isCode()){
            setCallback(result);
        }
        Toaster.show(result.getMsg());
    }

    @Override
    protected void oError() {

    }

    /***
     * 成功回调返回参数
     * @param result
     */
    private void setCallback(BaseResult<UserInfo> result) {
        UserInfo userInfo = result.getResult();
        String nickname = userInfo.getUser_nickname();
        appContext.getUserInfo().setUser_nickname(nickname);
        setResult(Constants.RESULT_CODE, new Intent().putExtra("nickname",nickname));
        this.finish();
    }



    @OnClick({R.id.head_right_title})
    public void onClicks(View view) {
        switch (view.getId()){
            /*保存*/
            case R.id.head_right_title:
                api.userUpdataNickname(getBEDstr(editTextNickname));
                break;
        }
    }

    /***
     * 设置保存按钮是否可点击
     * @param isClickable
     */
    private void setTextViewSave(boolean isClickable){
        textViewSave.setText("保存");
        textViewSave.setVisibility(View.VISIBLE);
        if(isClickable){
            textViewSave.setClickable(true);
            textViewSave.setTextColor(setBColor(R.color.color_3));
        }else{
            textViewSave.setClickable(false);
            textViewSave.setTextColor(setBColor(R.color.color_4));
        }

    }


    /***
     * 监听输入框
     */
    TextWatcher textWatcher = new TextWatcher() {
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override public void afterTextChanged(Editable s) {
            int num = s.toString().length();
            if(num > 0){
                setTextViewSave(true);
            }else {
                setTextViewSave(false);
            }
        }
    };
}
